package com.outsourcing.bottle.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.outsourcing.bottle.R;
import com.outsourcing.bottle.domain.MyFriendEntry;
import com.outsourcing.bottle.remoteimage.RemoteImageView;
import com.outsourcing.bottle.ui.UserSpaceActivity;
import com.outsourcing.bottle.ui.fragment.FriendFragment;
import com.outsourcing.bottle.util.TextUtil;

public class MyFriendListAdapter extends BaseAdapter {

	private Context context = null;
	private List<MyFriendEntry> mbtfriendList = null;

	public MyFriendListAdapter(Context context, FriendFragment fragment, List<MyFriendEntry> mbtfriendList) {
		this.context = context;
		this.mbtfriendList = mbtfriendList;
	}

	@Override
	public int getCount() {
		return mbtfriendList.size();
	}

	@Override
	public Object getItem(int position) {
		return mbtfriendList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		BTFriendViewHolder holder = null;
		if (convertView == null
				|| ((BTFriendViewHolder) convertView.getTag()).flag != position) {
			holder = new BTFriendViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.bottle_list_avatar_item, null);
			holder.flag = position;
			holder.mAuthorView = (RemoteImageView) convertView.findViewById(R.id.iv_author_photo);
			holder.mExType = (ImageView)convertView.findViewById(R.id.iv_bottle_type);
			holder.mExType.setVisibility(View.GONE);
			// content layout
			LinearLayout contentLayout = (LinearLayout) convertView
					.findViewById(R.id.feed_post_body);
			View view = LayoutInflater.from(context).inflate(
					R.layout.mygroupfriend_list_item, null);
			holder.mNickName = (TextView) view.findViewById(R.id.tv_onickname);
			holder.mSex = (ImageView)view.findViewById(R.id.bt_sex);
			holder.mDoing = (TextView) view.findViewById(R.id.tv_odoing);
			holder.mLocation = (TextView) view.findViewById(R.id.tv_olocation);
			
			contentLayout.addView(view);

			final MyFriendEntry myFriendEntry = (MyFriendEntry) getItem(position);

			try {
				holder.mAuthorView.setDefaultImage(R.drawable.avatar_default_big);
				holder.mAuthorView.setImageUrl(myFriendEntry.getFavatar());
			} catch (Exception e) {
				e.printStackTrace();
				holder.mAuthorView.setImageResource(R.drawable.avatar_default_big);
			}
			holder.mAuthorView.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent spaceIntent = new Intent(context, UserSpaceActivity.class);
					spaceIntent.putExtra("ouid", myFriendEntry.getFuid());
					context.startActivity(spaceIntent);
				}
			});
	
			String nickName = "<font color='#0099FF'>"
					+ myFriendEntry.getFnickname() + "</font>";
//			String sex = (((myFriendEntry.getFsex()) == 1) ? ("("
//					+ TextUtil.R("login_txt_register_male") + ")") : ("("
//					+ TextUtil.R("login_txt_register_female") + ")"));
			holder.mNickName.setText(Html.fromHtml(nickName));
			int sex_res = myFriendEntry.getFsex()== 1 ? R.drawable.male:R.drawable.female;
			holder.mSex.setBackgroundResource(sex_res);
			if (null != myFriendEntry.getFdoing()
					&& myFriendEntry.getFdoing().length() > 0) {
				holder.mDoing.setVisibility(View.VISIBLE);
				String doingTips = "<font color='#000000'>"
						+ TextUtil.R("friend_doing_tips") + "</font>";
				holder.mDoing.setText(Html.fromHtml(doingTips
						+ myFriendEntry.getFdoing()));
			} else {
				holder.mDoing.setVisibility(View.GONE);
			}
			if ((null != myFriendEntry.getFcountry() && myFriendEntry
					.getFcountry().length() > 0)
					|| (null != myFriendEntry.getFcity() && myFriendEntry
							.getFcity().length() > 0)) {
				holder.mLocation.setVisibility(View.VISIBLE);
				String fromTips = "<font color='#000000'>"
						+ TextUtil.R("friend_comfrom_tips") + "</font>";
				holder.mLocation.setText(Html.fromHtml(fromTips
						+ myFriendEntry.getFcity() + " "
						+ myFriendEntry.getFcountry()));

			} else {
				holder.mLocation.setVisibility(View.GONE);
			}
			
			convertView.setTag(holder);
		} else {
			holder = (BTFriendViewHolder) convertView.getTag();
		}

		return convertView;
	}


	public static class BTFriendViewHolder {
		public ImageView mSex;
		RemoteImageView mAuthorView;
		ImageView mExType;

		TextView mNickName;
		TextView mDoing;
		TextView mLocation;

		int flag;
	}

}
